from django.test import TestCase
from itsdangerous import TimedJSONWebSignatureSerializer as TJWSSerializer
from itsdangerous import BadData
# Create your tests here.

# itsdangerous: 进行数据的签名加密和解密
# 安装: pip install itsdangerous


if __name__ == "__main__":
    req_data = 'eyJleHAiOjE1NDg1ODA1NDYsImFsZyI6IkhTMjU2IiwiaWF0IjoxNTQ4NTc2OTQ2fQ.eyJvcGVuaWQiOiJLREtLU0RKSkRKVVExMjgzaUtETExEIn0.iugxCyCeJPr398tEw0ts1i8CjW-Cw82e_wAOlbfPdvE'

    # 解密
    serializer = TJWSSerializer(secret_key='123456abc')

    try:
        res_dict = serializer.loads(req_data)
    except BadData:
        print('解密失败')
    else:
        print(res_dict)

# if __name__ == "__main__":
#     my_dict = {
#         'openid': 'KDKKSDJJDJUQ1283iKDLLD'
#     }
#
#     # 签名加密
#     # TJWSSerializer(secret_key='密钥', expires_in='解密有效时间: s')
#     serializer = TJWSSerializer(secret_key='123456abc', expires_in=3600)
#     res = serializer.dumps(my_dict) # bytes
#
#     # bytes->str
#     res = res.decode()
#
#     print(res)





# if __name__ == "__main__":
#     # urlopen: 发起http请求
#     from urllib.request import urlopen
#
#     req_url = 'http://api.meiduo.site:8000/mobiles/13155667788/count/'
#
#     # 发起http请求
#     response = urlopen(req_url)
#
#     # 获取响应数据
#     res_data = response.read() # bytes
#     res_data = res_data.decode()
#     print(res_data)


# if __name__ == "__main__":
#     # parse_qs: 将查询字符串转换为字典
#     from urllib.parse import parse_qs
#
#     query_str = 'a=1&b=2&c=3'
#
#     res = parse_qs(query_str) # 注意：字典中key对应的value是list
#     # res['b'][0]
#     print(res)

# if __name__ == "__main__":
#     # urlencode: 将字典转换为查询字符串
#     from urllib.parse import urlencode
#
#     # a=1&b=2&c=3
#     req_dict = {
#         'a': 1,
#         'b': 2,
#         'c': 3
#     }
#
#     res = urlencode(req_dict)
#     print(res)
